package your_car_status

class LogInController {

    def index() { 
        redirect(action: "LogScreen", params: params)
    }
    def LogScreen(){
        println(session.permisions)
        if(session.permisions!=null){
            if(session.permisions=="RepairShop"){
                redirect(controller:"RepairShop", action: "newCustomer", params: params)
            }else if(session.permisions=="MechanicalTechnician"){
                redirect(controller:"MechanicalTechnician", action: "newVehicle", params: params)
            }else if(session.permisions=="Customer"){
                redirect(controller:"Customer", action: "listVehicles", params: params)
            }else if(session.permisions=="SuperAdmin"){
                redirect(controller:"RepairShop" ,action: "create", params: params)
            }
        }
        if(params.error==null){
            params.error=false
        }
        [error:params.error]
    }
    def LogIn(){
        def rs=RepairShop.findByUsername(params.Username)
        if(rs!=null){
            if(rs.password==params.Password){
                session["permisions"]="RepairShop"
                session.data=rs
                redirect(controller:"RepairShop", action: "newCustomer", params: params)
            }else{
                params.error=true
                redirect(action: "LogScreen", params: params)
            }
        }else{          
            def mt=MechanicalTechnician.findByUsername(params.Username)
            if(mt!=null){    
                if(mt.password==params.Password){
                    session["permisions"]="MechanicalTechnician"
                    session.data=mt
                    redirect(controller:"MechanicalTechnician", action: "newVehicle")
                }else{
                    params.error=true
                    redirect(action: "LogScreen", params: params)
                }
            }else{
                def cust=Customer.findByEmail(params.Username)
                if(cust!=null){    
                    if(cust.password==params.Password){
                        session["permisions"]="Customer"
                        session.data=cust
                        redirect(controller:"Customer" ,action: "listVehicles", params: params,id:cust.id)
                    }else{
                        params.error=true
                        redirect(action: "LogScreen", params: params)
                    }
                }else{
                    if(params.Password=="123456" && params.Username=="YCS"){
                        session["permisions"]="SuperAdmin"
                        session.data=new RepairShop([id: 0,name: "YCS",username: "YCS",address: "Address",phone: "7777777",password: "123456"])
                        redirect(controller:"Admin" ,action: "newRepairShop", params: params)
                    }else{
                        params.error=true
                        redirect(action: "LogScreen", params: params)
                    }
                }
            }
        }
    }
    
    def logout() {
        session.invalidate()
        redirect(action: "LogScreen")
    }
}
